﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        TcpClient server = new TcpClient();
        Thread s;
        String key = "c";
        public Form1()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String hostname = textBox1.Text;
            String port = textBox2.Text;
            if (server.Connected)
            {
                MessageBox.Show("Already Connected!");
                return;
            }
            server.Connect(hostname, Convert.ToInt32(port));
            textBox1.Enabled = textBox2.Enabled = textBox5.Enabled = false;
            s = new Thread(new ThreadStart(StartConnecting));
            s.Start();

            StreamWriter writer = new StreamWriter(server.GetStream());
            writer.WriteLine(Crypt.Encrypt("Connect " + textBox5.Text, key));
            writer.Flush();
        }

        public void StartConnecting()
        {
            while (true)
            {
                if (server.Connected)
                {
                    try
                    {
                        StreamReader reader = new StreamReader(server.GetStream());
                        StreamWriter writer = new StreamWriter(server.GetStream());
                        String cmd = "";
                        bool running = true;
                        while (running)
                        {
                            String[] cmnd;
                            cmd = reader.ReadLine();
                            if (cmd == null || cmd.Length == 0)
                                continue;
                            cmd = Crypt.Decrypt(cmd, key);
                            if ((cmnd = cmd.Split(' ')).Length < 2)
                                continue;
                            switch (cmnd[0])
                            {
                                case "Connect":
                                    break;
                                case "Users":
                                    String list = cmnd[1];
                                    listBox1.Items.Clear();
                                    foreach (String na in list.Split('|'))
                                    {
                                        if (na.Trim().Length > 0)
                                            listBox1.Items.Add(na);
                                    }
                                    listBox1.SetSelected(0, true);
                                    break;
                                case "Receive":
                                    textBox3.AppendText(cmd.Replace("Receive ", "") + Environment.NewLine);
                                    break;
                            }
                        }
                    }
                    catch (Exception ea) { }
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (server.Connected)
            {
                StreamWriter writer = new StreamWriter(server.GetStream());
                writer.WriteLine(Crypt.Encrypt("Disconnect ", key));
                writer.Flush();
                writer.Close();
                server.Close();
                server = new TcpClient();
                if (s != null)
                    s.Abort();
                textBox1.Enabled = textBox2.Enabled = textBox5.Enabled = true;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (server.Connected)
            {
                StreamWriter writer = new StreamWriter(server.GetStream());
                writer.WriteLine(Crypt.Encrypt("Disconnect ", key));
                writer.Flush();
                writer.Close();
                server.Close();
                if (s != null)
                    s.Abort();
                textBox1.Enabled = textBox2.Enabled = textBox5.Enabled = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem != null)
            {
                String selected = listBox1.SelectedItem.ToString();
                String ip = selected.Split(':')[1];
                String name = selected.Split(':')[0];
                if (server.Connected)
                {
                    StreamWriter writer = new StreamWriter(server.GetStream());
                    String messs = textBox4.Text;
                    String l = "Send " + ip + " " + name + " " + messs;
                    textBox3.AppendText("Me: " + messs + Environment.NewLine);
                    writer.WriteLine(Crypt.Encrypt(l, key));
                    writer.Flush();
                    textBox4.Text = "";
                }
            }
        }

        private void textBox4_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button2_Click(new object(), null);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }
    }
}